package com.partsoft.spring.webapp;

import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;

public abstract class WebUtils extends org.springframework.web.util.WebUtils {

	private static Map<String, String> bbMap = new HashMap<String, String>();

	static {
		bbMap.put("(\r\n|\r|\n|\n\r)", "<br/>");
		
		bbMap.put("\\[b\\]", "<strong>");
		bbMap.put("\\[/b\\]", "</strong>");
		
		bbMap.put("\\[i\\]", "<i>");
		bbMap.put("\\[/i\\]", "</i>");
		
		bbMap.put("\\[u\\]", "<u>");
		bbMap.put("\\[/u\\]", "</u>");
		
		bbMap.put("\\[h1\\]", "<h1>");
		bbMap.put("\\[/h1\\]", "</h1>");

		bbMap.put("\\[h2\\]", "<h2>");
		bbMap.put("\\[/h2\\]", "</h2>");

		bbMap.put("\\[h3\\]", "<h3>");
		bbMap.put("\\[/h3\\]", "</h3>");

		bbMap.put("\\[h4\\]", "<h4>");
		bbMap.put("\\[/h4\\]", "</h4>");

		bbMap.put("\\[h5\\]", "<h5>");
		bbMap.put("\\[/h5\\]", "</h5>");

		bbMap.put("\\[h6\\]", "<h6>");
		bbMap.put("\\[/h6\\]", "</h6>");
		
		bbMap.put("\\[quote\\]", "<blockquote>");
		bbMap.put("\\[/quote\\]", "</blockquote>");
		
		bbMap.put("\\[p\\]", "<p>");
		bbMap.put("\\[/p\\]", "</p>");
		
		bbMap.put("\\[p=(.+?),(.+?)\\]", "<p style='text-indent:$1px;line-height:$2%;'>");
		bbMap.put("\\[/p\\]", "</p>");
		
		bbMap.put("\\[center\\]", "<div align='center'>");
		bbMap.put("\\[/center\\]", "</div>");
		
		bbMap.put("\\[align=(.+?)\\]", "<div align='$1'>");
		bbMap.put("\\[/align\\]", "</div>");
		
		bbMap.put("\\[color=(.+?)\\]", "<span style='color:$1;'>");
		bbMap.put("\\[/color\\]", "</span>");
		
		bbMap.put("\\[font=(.+?)\\]", "<font face='$1'>");
		bbMap.put("\\[/font\\]", "</span>");
		
		bbMap.put("\\[size=(.+?)\\]", "<font size='$1'>");
		bbMap.put("\\[/size\\]", "</font>");
		
		bbMap.put("\\[attachment=(.*?)\\]", "");
		
		bbMap.put("\\[img\\](.*?)\\[/img\\]", "<img src='$1' />");
		
		bbMap.put("\\[img=(.+?),(.+?)\\](.*?)\\[/img\\]", "<img width='$1' height='$2' src='$3' />");
		
		bbMap.put("\\[flash\\](.*?)\\[/flash\\]", "");
		
		bbMap.put("\\[flash=(.+?),(.+?)\\](.*?)\\[/flash\\]", "");
		
		bbMap.put("\\[email\\](.*?)\\[/email\\]", "<a href='mailto:$1'>$1</a>");
		
		bbMap.put("\\[email=(.+?)\\](.*?)\\[/email\\]", "<a href='mailto:$1'>$2</a>");
		
		bbMap.put("\\[url\\](.*?)\\[/url\\]", "<a href='$1'>$1</a>");
		
		bbMap.put("\\[url=(.+?)\\](.*?)\\[/url\\]", "<a href='$1'>$2</a>");
		
		bbMap.put("\\[attachment=(.*?)\\]", "");
		
		bbMap.put("\\[backcolor=(.*?)\\]", "");
		bbMap.put("\\[/backcolor\\]", "");
		
		bbMap.put(
				"\\[youtube\\](.+?)\\[/youtube\\]",
				"<object width='640' height='380'><param name='movie' value='http://www.youtube.com/v/$1'></param><embed src='http://www.youtube.com/v/$1' type='application/x-shockwave-flash' width='640' height='380'></embed></object>");
		bbMap.put("\\[video\\](.+?)\\[/video\\]", "<video src='$1' />");
	}

	public static ResponseEntity<String> buildResponse(String bodyMsg, Charset charset, HttpStatus status) {
		HttpHeaders headers = new HttpHeaders();
		MediaType mt = new MediaType("text", "html", charset);
		headers.setContentType(mt);
		return new ResponseEntity<String>(bodyMsg, headers, status);
	}

	public static ResponseEntity<String> buildResponse(String bodyMsg, HttpStatus status) {
		return buildResponse(bodyMsg, Charset.forName("utf-8"), status);
	}

	public static ResponseEntity<String> buildResponse(String bodyMsg) {
		return buildResponse(bodyMsg, Charset.forName("utf-8"), HttpStatus.OK);
	}

	public static String bbCode2Html(String text) {
		String html = text;
		for (Map.Entry<String, String> entry : bbMap.entrySet()) {
			html = html.replaceAll(entry.getKey(), entry.getValue());
		}
		return html;
	}
	
}
